//
//  MYG.swift
//  MYDoctor
//
//  Created by Denny on 2024/3/8.
//

import UIKit

extension UIView {
    func rootResponsder()->UIViewController{
        var vc:UIResponder = self
        while (vc is UIViewController) != true {
            vc = vc.next ?? UIViewController()
        }
        return vc as? UIViewController ?? UIViewController()
    }
    
    //添加圆角
    func addCorner(conrners: UIRectCorner,radius: CGFloat,boradColor:UIColor,boradWidth:CGFloat) {
        DispatchQueue.main.asyncAfter(deadline: .now()+0.01) {
            self.layoutIfNeeded()
            let maskLayer = CAShapeLayer()
            maskLayer.frame = CGRect.init(x: 0, y: 0, width: self.bounds.size.width, height: self.bounds.size.height)
            //边框
            let borderLayer = CAShapeLayer()
            borderLayer.frame = CGRect.init(x: 0, y: 0, width: self.bounds.size.width, height: self.bounds.size.height)
            borderLayer.lineWidth = boradWidth
            borderLayer.strokeColor = boradColor.cgColor
            borderLayer.fillColor = UIColor.clear.cgColor
            //指定圆角
            let bezierPath = UIBezierPath(roundedRect: self.bounds, byRoundingCorners: conrners, cornerRadii: CGSize(width: radius, height: radius))
            maskLayer.path = bezierPath.cgPath
            borderLayer.path = bezierPath.cgPath
            self.layer.insertSublayer(borderLayer, at: 0)
            self.layer.mask = maskLayer
        }
    }
    
    
    //添加部分边框
    
    func addBordorLine(top:Bool,right:Bool,bottom:Bool,left:Bool,color:UIColor,width:CGFloat){
        if let sublayers = self.layer.sublayers {
            for (_,sub) in sublayers.enumerated() {
                if sub.isKind(of: CAShapeLayer.self) {
                    sub.removeFromSuperlayer()
                }
            }
        }
        if bottom {
            DispatchQueue.main.asyncAfter(deadline: .now() + 0.2, execute: {
                let layer = CAShapeLayer.init()
                layer.frame = CGRect.init(x: 0, y: self.frame.size.height - width, width: self.frame.size.width, height: width)
                layer.backgroundColor = color.cgColor
                self.layer.addSublayer(layer)
            })
        }
        if top {
            DispatchQueue.main.asyncAfter(deadline: .now() + 0.2, execute: {
                let layer = CAShapeLayer.init()
                layer.frame = CGRect.init(x: 0, y: 0, width: self.frame.size.width, height: width)
                layer.backgroundColor = color.cgColor
                self.layer.addSublayer(layer)
            })
        }
        if left {
            DispatchQueue.main.asyncAfter(deadline: .now() + 0.2, execute: {
                let layer = CAShapeLayer.init()
                layer.frame = CGRect.init(x: 0, y: 0, width: width, height: self.frame.size.height)
                layer.backgroundColor = color.cgColor
                self.layer.addSublayer(layer)
            })
        }
        if right {
            DispatchQueue.main.asyncAfter(deadline: .now() + 0.2, execute: {
                let layer = CAShapeLayer.init()
                layer.frame = CGRect.init(x: self.frame.size.width - width, y: 0, width: width, height: self.frame.size.height)
                layer.backgroundColor = color.cgColor
                self.layer.addSublayer(layer)
            })
        }
    }
    func addCorner(radius: CGFloat) {
        self.layer.cornerRadius = radius
        self.layer.masksToBounds = true
    }
    
    /**
     添加渐变色
     颜色数组
     开始点
     结束点
     */
    
    func addCAGradientLayerColor(colors:[CGColor],
                                   startPoint:CGPoint,
                                   endPoint:CGPoint) -> Void {
        
        DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
            self.layoutIfNeeded()
            let layer = CAGradientLayer()
            layer.colors = colors
            layer.startPoint = startPoint
            layer.endPoint = endPoint
            layer.frame = self.bounds
            self.layer.addSublayer(layer)
            self.layer.insertSublayer(layer, at: 0)
        }
    }
   
    /**
     添加带圆角的阴影
     */
    func addRoundedOrShadow(radius:CGFloat,backGroundColor:UIColor, shadowOpacity:CGFloat, shadowColor:UIColor)  {
        self.layer.cornerRadius = radius
        self.layer.masksToBounds = true
        let subLayer = CALayer()
        let fixframe = self.frame
        let newFrame = CGRect(x: fixframe.minX-(375-UIScreen.main.bounds.size.width)/2, y: fixframe.minY, width: fixframe.width, height: fixframe.height) // 修正偏差
        subLayer.frame = newFrame
        subLayer.cornerRadius = radius
        subLayer.backgroundColor = backGroundColor.cgColor
        subLayer.masksToBounds = false
        subLayer.shadowColor = shadowColor.cgColor // 阴影颜色
        subLayer.shadowOffset = CGSize(width: 0, height: 0) // 阴影偏移,width:向右偏移3，height:向下偏移2，默认(0, -3),这个跟shadowRadius配合使用
        subLayer.shadowOpacity = Float(shadowOpacity) //阴影透明度
        subLayer.shadowRadius = 5;//阴影半径，默认3
//        self.layer.i
        self.layer.insertSublayer(subLayer, at: 0)

//        self.layer.insertSublayer(subLayer, below: self.layer)
//        self.layer.insertSublayer(subLayer, at: 0)
    }

    func createToImage() -> UIImage? {
        if #available(iOS 10.0, *) {
            let renderer = UIGraphicsImageRenderer(bounds: bounds)
            return renderer.image { rendererContext in
                layer.render(in: rendererContext.cgContext)
            }
        } else {
            UIGraphicsBeginImageContextWithOptions(bounds.size, false, UIScreen.main.scale)
            defer { UIGraphicsEndImageContext() }
            if let context = UIGraphicsGetCurrentContext() {
                layer.render(in: context)
                return UIGraphicsGetImageFromCurrentImageContext()
            }
        }
        return nil
    }
}
